package com.epfresh.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.bean.NewHomeBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Chen
 * Date: 2019/4/22
 * Desc:
 */
public class LimitedTimeSpecialAdapter extends RecyclerView.Adapter<LimitedTimeSpecialAdapter.ViewHolder> {
    private List<NewHomeBean.SpikeBean> goods = new ArrayList<>();
    private String seckillTotal;
    private OnLimitedTimeGoodClickListener onLimitedTimeGoodClickListener;

    public void setOnLimitedTimeGoodClickListener(OnLimitedTimeGoodClickListener onLimitedTimeGoodClickListener) {
        this.onLimitedTimeGoodClickListener = onLimitedTimeGoodClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_limited_time_special_good, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        ImageLoader.getInstance().displayImage(goods.get(position).getProduct().getThumbnail(), holder.iv_product);
        ImageLoader.getInstance().displayImage(goods.get(position).getPromotionMarkUrl(), holder.iv_tag);
        if (goods.get(position).getProduct().getName() != null && !goods.get(position).getProduct().getName().equals("默认")) {
            holder.txt_package.setText(goods.get(position).getProduct().getName() + "(" + goods.get(position).getProduct().getPack() + ")");
        } else {
            holder.txt_package.setText(goods.get(position).getProduct().getPack());
        }
        holder.txt_product_title.setText(goods.get(position).getProduct().getProductParseDto().getTitle());
        holder.txt_price.setText("¥" + goods.get(position).getPrice() + "/" + goods.get(position).getProduct().getUnit());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onLimitedTimeGoodClickListener != null) {
                    onLimitedTimeGoodClickListener.onLimitedTimeGoodClick(goods.get(holder.getAdapterPosition()));
                }
            }
        });
        holder.rl_center_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onLimitedTimeGoodClickListener != null) {
                    onLimitedTimeGoodClickListener.showMore();
                }
            }
        });
        if (position == goods.size() - 1) {
            holder.rl_center_more.setVisibility(View.VISIBLE);
            holder.tv_center_number.setText("共" + seckillTotal + "件");
        } else {
            holder.rl_center_more.setVisibility(View.GONE);
        }

        if (goods.get(position).getStatus() == 4) {
            holder.tv_finish_flag.setVisibility(View.VISIBLE);
        } else {
            holder.tv_finish_flag.setVisibility(View.GONE);
        }

    }

    @Override
    public int getItemCount() {
        return goods.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_product, iv_tag;
        private TextView txt_product_title, txt_package, txt_price, tv_center_number, tv_finish_flag;
        private View rl_center_more;

        ViewHolder(View itemView) {
            super(itemView);

            txt_product_title = itemView.findViewById(R.id.txt_product_title);
            iv_product = itemView.findViewById(R.id.iv_product);
            iv_tag = itemView.findViewById(R.id.iv_tag);
            txt_package = itemView.findViewById(R.id.txt_package);
            txt_price = itemView.findViewById(R.id.txt_price);
            rl_center_more = itemView.findViewById(R.id.rl_center_more);
            tv_center_number = itemView.findViewById(R.id.tv_center_number);
            tv_finish_flag = itemView.findViewById(R.id.tv_finish_flag);
        }
    }

    public void setGoods(List<NewHomeBean.SpikeBean> goods,String seckillTotal) {
        this.seckillTotal = seckillTotal;
        this.goods.clear();
        this.goods.addAll(goods);
        notifyDataSetChanged();
    }

    public interface OnLimitedTimeGoodClickListener {
        void onLimitedTimeGoodClick(NewHomeBean.SpikeBean spikeBean);
        void showMore();
    }

}
